﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication1
{
    public class DataGridViewEx : DataGridView
    {
        private string sortStr = "";
        private ImageList imgs;
        private System.ComponentModel.IContainer components;

        public string SortString
        {
            get { return sortStr; }
        }

        public DataGridViewEx()
        {
            InitializeComponent();
        }

        public void Sort()
        {
            throw new NotImplementedException();
        }

        public void Sort(string sort)
        {
            if (sort != sortStr)
            {
                sortStr = sort;
                Sort();
            }
        }

        protected override void OnRowPrePaint(DataGridViewRowPrePaintEventArgs e)
        {
            base.OnRowPrePaint(e);
        }
        protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        {
            base.OnRowPostPaint(e);
        }
        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            base.OnCellPainting(e);
            if (e.RowIndex == -1)
            {
                bool mouseOver = e.CellBounds.Contains(this.PointToClient(Cursor.Position));
                LinearGradientBrush brush = new LinearGradientBrush(
                    e.CellBounds,
                    mouseOver ? Color.FromArgb(120, 200, 250) : Color.FromArgb(154, 174, 222),
                    Color.FromArgb(240, 240, 240),
                    LinearGradientMode.Vertical);
                e.Graphics.FillRectangle(brush, e.CellBounds);
                Rectangle border = e.CellBounds;
                border.Width -= 1;
                e.Graphics.DrawRectangle(Pens.LightGray, border);

                e.PaintContent(e.CellBounds);
                e.Handled = true;
            }
        }
        protected override void OnCellClick(DataGridViewCellEventArgs e)
        {
            base.OnCellClick(e);
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DataGridViewEx));
            this.imgs = new System.Windows.Forms.ImageList(this.components);
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            // 
            // imgs
            // 
            this.imgs.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgs.ImageStream")));
            this.imgs.TransparentColor = System.Drawing.Color.Transparent;
            this.imgs.Images.SetKeyName(0, "ar_b.png");
            this.imgs.Images.SetKeyName(1, "ar_g.png");
            this.imgs.Images.SetKeyName(2, "ar_r.png");
            this.imgs.Images.SetKeyName(3, "arr.png");
            // 
            // DataGridViewEx
            // 
            this.RowTemplate.Height = 21;
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);

        }
    }

    internal class SortComparer : System.Collections.IComparer
    {
        private Regex regexASC;
        private Regex regexDESC;

        #region IComparer メンバ

        int System.Collections.IComparer.Compare(object x, object y)
        {
            int result = 0;

            DataGridViewRow row1 = x as DataGridViewRow, row2 = y as DataGridViewRow;
            DataGridViewEx dgv = row1.DataGridView as DataGridViewEx;
            string[] sorts = dgv.SortString.Split(',');
            for (int i = 0; i < sorts.Length; i++)
            {
                int asc_desc = 1;
                string [] parts = sorts[i].Trim().Split(' ');
                string field = "";
                if (parts.Length > 0)
                {
                    field = parts[0];
                    if (parts.Length > 1)
                    {
                        if (parts[1].ToUpper() == "ASC")
                            asc_desc = 1;
                        else if (parts[1].ToUpper() == "DESC")
                            asc_desc = -1;
                        else
                            asc_desc = 1;
                    }
                    else
                        asc_desc = 1;
                    if (dgv.Columns.Contains(field))
                    {
                        object val1 = row1.Cells[field].Value;
                        object val2 = row2.Cells[field].Value;
                        if (row1.Cells[field].Value.ToString() == "" && row2.Cells[field].Value.ToString() == "")
                            continue;
                        //else (row1.Cells[field].Value is Decimal || row1.Cells
                    }
                }

            }

            return result;
        }

        #endregion

        public SortComparer()
        {
            regexASC = new Regex("[ ]ASC$", RegexOptions.IgnoreCase);
            regexDESC = new Regex("[ ]DESC$", RegexOptions.IgnoreCase);
        }
    }
}
